/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.compare');
(function() {
    var DQ = Ext.DomQuery;

    /**
    * The EmptyCompetitorView is used when no vehicle is selected
    * View class used to control how the net.autodata.compare.VehiclePanel is rendered	 * 
    * @class EmptyCompetitorView
    * @extends net.autodata.AbstractView
    * @namespace net.autodata.compare
    */
    var View = Ext.extend(net.autodata.AbstractView, {

        /**
        * @property changeButton
        * @type net.autodata.compare.ChangeButton
        */
        changeButton: undefined,
        removeButton: undefined,

        modelWalk: undefined,

        modelYearComboWidth: 75,
        comboWidth: 140,

        //override abstract constructor to allow the object to be created
        constructor: function(cfg) {

            cfg = cfg || {};
            Ext.apply(this, cfg);

            this.initialConfig = cfg;

            //call ctor to ensure the Base Classes are created correctly
            View.superclass.ctor.apply(this, arguments);
        },

        /**
        * @method init
        */
        init: function() {
            View.superclass.init.apply(this, arguments);
            this.initComponents();
        },

        /**
        * @method initComponents
        */
        initComponents: function() {
            this.changeButton = new net.autodata.compare.ChangeButton({
                listeners: {
                    'trimselect': this.onTrimSelect,
                    scope: this
                }
            });

            this.removeButton = new net.autodata.compare.RemoveButton({ disabled: true });

            var loader = this.getModelWalkLoader();

            this.modelWalk = new net.autodata.modelwalk.ModelWalkPanel({
                view: new net.autodata.modelwalk.ModelWalkMenuView(),
                plugins: loader,
                loader: loader
            });

            Ext.iterate(this.modelWalk.dependencyMap, function(p, v) {
                if (p !== 'modelYear') {
                    this.modelWalk[p].setWidth(this.comboWidth);
                } else {
                    this.modelWalk[p].setWidth(this.modelYearComboWidth || this.comboWidth);
                }
            }, this);

            this.modelWalk.trim.on('select', this.onModelWalkTrimSelect, this);
        },

        /**
        * @method initTemplates
        */
        initTemplates: function() {
            var ts = this.templates || {};

            if (!ts.mainTpl) {
                ts.mainTpl = new Ext.Template(
					'<div class="emptyCompetitor-view">',
						'<div class="vehiclepanel-header">',
							'<table border="0" cellpadding="0" cellspacing="0">',
								'<tr>',
									'<td class="change-btn-wrap"></td>',
									'<td class="remove-btn-wrap"></td>',
								'</tr>',
							'</table>',
						'</div>',
						'<div class="vehiclepanel-body">',
						'</div>',
					'</div>',
					{
					    compiled: true,
					    disableFormats: true
					}
				);
            }

            this.templates = ts;

            View.superclass.initTemplates.apply(this, arguments);
        },

        /**
        * @method getModelWalkLoader
        * Returns a reference to Plugin to be used to load the modelwalk data
        */
        getModelWalkLoader: function() {
            if (!this.modelWalkLoader) {
                this.modelWalkLoader = new net.autodata.modelwalk.plugins.ModelYearLoader();
            }
            return this.modelWalkLoader;
        },

        /**
        * @method render
        */
        render: function() {
            View.superclass.render.apply(this, arguments);
            var dom = this.component.el.dom;

            this.templates.mainTpl.overwrite(dom, {});
            this.changeButton.render(DQ.selectNode('.vehiclepanel-header td.change-btn-wrap', dom));
            this.removeButton.render(DQ.selectNode('.vehiclepanel-header td.remove-btn-wrap', dom));
            this.modelWalk.render(DQ.selectNode('.vehiclepanel-body', dom));

            var loader = this.getModelWalkLoader();
            if (loader && loader.doLoad) {
                loader.doLoad();
            }
        },

        onModelWalkTrimSelect: function(field, record) {
            var acode = record ? record.id : '';
            this.onTrimSelect(acode);
        },

        onTrimSelect: function(acode) {
            if (!Ext.isEmpty(acode)) {
                this.fireEvent('change', acode);
            }
        },

        onDestroy: function() {
            this.changeButton.un('trimselect', this.onTrimSelect, this);
            this.modelWalk.trim.un('select', this.onModelWalkTrimSelect, this);

            View.superclass.onDestroy.apply(this, arguments);
        }

    });

    net.autodata.compare.EmptyCompetitorView = View;
})();